import {
    getBanner,
    getNewShop,
    getRecommend,
    getInfo,
    getIpadList,
    getsearch,
    getLogin,
    getCart,
    addCart,
    delCart,
    clearCart
} from '../../api/app'
// 定义状态值
const state = {
    banner: [],
    newshop: {},
    recommend: {},
    infos: {},
    ipadlist: {},
    search: {},
    login: {},
    cartlist: {},
    addcarts: {},
    delcarts: {},
}
// 修改state状态值
const mutations = {
    SET_BANNER: (state, arr) => {
        state.banner = arr
    },
    SET_NEWSHOP: (state, data) => {
        state.newshop = data
    },
    SET_RECOMMEND: (state, data) => {
        state.recommend = data
    },
    SET_INFO: (state, data) => {
        state.infos = data
    },
    SET_IPAD: (state, data) => {
        state.ipadlist = data
    },
    SET_SEARCH: (state, data) => {
        state.search = data
    },
    SET_LOGIN: (state, data) => {
        state.login = data
    },
    SET_CARTLIST: (state, data) => {
        state.cartlist = data
    },
    SET_CART: (state, data) => {
        state.addcarts = data
    },
    SET_DELCART: (state, data) => {
        state.delcarts = data
    }
}
const actions = {
    // 轮播接口接口调用
    BannerList({ commit }) {
        return new Promise((res, rej) => {
            var getbanner = getBanner()
            if (getbanner) {
                commit("SET_BANNER", getbanner)
                res(getbanner)
            }
        })
    },
    // 最新商品接口调用
    NewShopList({ commit }) {
        return new Promise((res, rej) => {
            var getnewshop = getNewShop()
            if (getnewshop) {
                commit("SET_BANNER", getnewshop)
                res(getnewshop)
            }
        })
    },
    // 推荐商品接口
    Recommend({ commit }) {
        return new Promise((res, rej) => {
            var getrecommendshop = getRecommend()
            if (getrecommendshop) {
                commit("SET_RECOMMEND", getrecommendshop)
                res(getrecommendshop)
            }
        })
    },
    // 详情页接口调用
    InFo({ commit }, data) {
        return new Promise((res, rej) => {
            var getinfos = getInfo(data)
            if (getinfos) {
                commit("SET_RECOMMEND", getinfos + data)
                res(getinfos)
            }
        })
    },
    // 分类商品接口调用
    IpadList({ commit }, data) {
        return new Promise((res, rej) => {
            var getipad = getIpadList(data)
            if (getipad) {
                commit("SET_IPAD", getipad)
                res(getipad)
            }
        })
    },
    // 搜索接口
    Search({ commit }, data) {
        return new Promise((res, rej) => {
            var getSearch = getsearch(data)
            if (getSearch) {
                commit("SET_SEARCH", getSearch + data)
                res(getSearch)
            }
        })
    },
    // 登录接口
    Login({ commit }, data) {
        return new Promise((res, rej) => {
            var getlogins = getLogin(data)
            if (getlogins) {
                commit("SET_SEARCH", getlogins + data)
                res(getlogins)
            }
        })
    },
    // 获取购物车列表
    CartList({ commit }, data) {
        return new Promise((res, rej) => {
            var getcartlist = getCart(data)
            if (getcartlist) {
                commit("SET_CARTLIST", getcartlist + data)
                res(getcartlist)
            }
        })
    },
    // 添加购物车接口调用
    AddCart({ commit }, data) {
        return new Promise((res, rej) => {
            var addcartlist = addCart(data)
            if (addcartlist) {
                commit("SET_CART", addcartlist + data)
                res(addcartlist)
            }
        })
    },
    // 删除购物车接口调用
    DelCart({ commit }, data) {
        return new Promise((res, rej) => {
            var delcartlist = delCart(data)
            if (delcartlist) {
                commit("SET_DELCART", delcartlist + data)
                res(delcartlist)
            }
        })
    },
    // 清除购物车
    ClearCart({ commit },data){
        return new Promise((res, rej) => {
            var clearcart = clearCart(data)
            if (clearcart) {
                res(clearcart)
            }
        })
    }
}
export default {
    // 获取属性的同时要加上名称
    namespaced: true,
    state,
    mutations,
    actions
}
